__author__ = 'huskier'
# -*- coding: utf-8 -*-

import matplotlib.pyplot as plt
import matplotlib
import numpy as np
from plot_figure import get_freq_amp_of_data_by_fft

myfont = matplotlib.font_manager.FontProperties(fname='/usr/share/fonts/myfonts/simfang.ttf')#windows下的仿宋
"""
1.Just download the fonts you want from free online web sites and archives (listed below),
 if necessary – unzip them to the desktop or another directory of your choice.
2.Then create a directory for your new fonts where the fonts are stored on your system. This
is the location in Ubuntu Linux – your Linux distribution may be similar:
$ sudo mkdir /usr/share/fonts/myfonts
3.Then open your terminal to install them:
$ sudo cp /home/user/Desktop/*.ttf /usr/share/fonts/myfonts/
4.Refresh your font cache like this:
$ sudo fc-cache -f
"""

fs = 100000*1 #freq sample
t=np.arange(0,0.6,1.0/fs)
f1 = 100
f2 = 300
x = np.sin(2*np.pi*f1*t) + np.sin(2*np.pi*f2*t)

fig = plt.figure()
ax = fig.add_subplot(711)
ax.plot(x)
ax.set_title(ur"f1 (100Hz)\f2(300Hz)的正弦信号，初相0",fontproperties=myfont)
ax.set_xlabel(ur'序列（n）',fontproperties=myfont)
ax.grid(True)


number = 512

y = np.fft.fft(x,number)#len(x)为600，len(y)为number

n = np.arange(0,len(y))
f = fs*n/len(y)
ax1 = fig.add_subplot(713)
plt.plot(f,np.abs(y))

y1 =np.fft.fft(x)
n1 =np.arange(0,len(y1))
f1 = fs*n1/len(y1)
plt.plot(f1,np.abs(y1))
ax1.set_title(ur'f1\f2的正弦信号的FFT（512点）',fontproperties=myfont)
ax1.set_xlabel('freq(Hz)')
ax1.grid(True)


x=np.add(x, np.random.rand(len(x)))
ax2 = fig.add_subplot(715)
ax2.plot(x)
ax2.set_title(ur'原f1\f2的正弦信号（含随机噪声）',fontproperties=myfont)
ax2.set_xlabel(ur'序列（n）',fontproperties=myfont)
ax2.grid(True)


y=np.fft.fft(x,number)

n=np.arange(0,len(y))
f=fs*n/len(y)
ax3 = fig.add_subplot(717)
ax3.plot(f,np.abs(y))
ax3.set_title(ur'原f1\f2的正弦信号（含随机噪声）的FFT（512点）',fontproperties=myfont)
ax3.set_xlabel(ur'频率Hz',fontproperties=myfont)
ax3.grid(True)


p=2+3*np.cos(2*np.pi*30*t-np.pi*30/180)+1.5*np.cos(2*np.pi*75*t+np.pi*90/180)+9*np.cos(2*np.pi*2230*t+np.pi*90/180)
#p=200+100*np.cos(2*np.pi*32*t-np.pi*30/180)
print len(p),len(t)
bf,b = get_freq_amp_of_data_by_fft(p,sample_period=1./fs,number_of_fft=60000)

#b=np.abs(np.fft.fft(p))
#N = len(p)
#time_step = 1./fs
#bf = np.fft.fftfreq(N,d=time_step)

print max(b)
ax4 = fig.add_subplot(716)
ax4.plot(bf[:10000],b[:10000],)
ax5=ax4.twinx()
ax5.plot(p,'ro')

b1=np.fft.fft(p)
recover=np.fft.ifft(b1)
ax5.plot(recover,'y')
#ax4.plot(p)


plt.show()